home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / destroy4.cpp < prev    next >
C/C++ Source or Header  |  1994-05-02  |  677b  |  47 lines

  1. LISTING 8 - Deallocates a resource in the midst of handling an
  2. exception
  3.  
  4. // destroy4.cpp
  5. #include <stdio.h>
  6.  
  7. void f(char *fname);
  8.  
  9. main()
  10. {
  11.     try
  12.     {
  13.         f("file1.dat");
  14.     }
  15.     catch(...)
  16.     {
  17.         puts("Exception caught in main()");
  18.     }
  19.     return 0;
  20. }
  21.  
  22. void f(char *fname)
  23. {
  24.     FILE *fp = fopen(fname,"r");
  25.     if (fp)
  26.     {
  27.         try
  28.         {
  29.             throw 1;
  30.         }
  31.         catch (...)
  32.         {
  33.             fclose(fp);
  34.             puts("File closed");
  35.             throw;  // Re-throw
  36.         }
  37.  
  38.         fclose(fp); // The normal close
  39.     }
  40. }
  41.  
  42. /* Output:
  43. File closed
  44. Excepion caught in main()
  45. */
  46.  
  47.